
// 数字累加
+function ($) {
	'use strict';

	var Accumulate = function (element, options) {
		this.$element = $(element);
		this.options = $.extend({}, Accumulate.DEFAULTS, options);
		this.visible = false;
		this.waitTimer = null;
		this.execTimer = null;
		this.number = this.options.number;
		if (this.number == undefined) {
			this.number = this.$element.text().replace(/[,\s]/g, "") * 1;
		} else {
			this.$element.text(this.formatNumber(this.number));
		}
		this.bind();
		this.init();
	};

	Accumulate.DEFAULTS = {
		auto: true,
		pause: false,
		number: null, // 初始值
		offset: 100, // 每次递增值: 100 or [100, 1000]
		interval: 1000, // 间隔时间: undefined or number or array
		duration: 100,
		runOnVisible: false // 每出现在可视窗口时运行一次
	};

	Accumulate.prototype.pause = function (pause) {
		this.options.pause = !!pause;
		if (this.options.pause) {
			if (this.waitTimer != null) {
				window.clearTimeout(this.waitTimer);
				this.waitTimer = null;
			}
		} else {
			if (this.options.auto && this.waitTimer == null) {
				this.init();
			}
		}
	};

	Accumulate.prototype.formatNumber = function(number) {
		if (!number || number < 1000) { return number; }
	    var temp = number.toString();
	    var result = "";
	    while (temp.length > 3) {
	        result = ',' + temp.slice(-3) + result;
	        temp = temp.slice(0, temp.length - 3);
	    }
	    if (temp) { result = temp + result; }
	    return result;
	};
	Accumulate.prototype.run = function() {
		if (this.execTimer != null) { return; }
		var offset = this.options.offset;
		if ($.isArray(offset)) { offset = Randoms.number(offset[0], offset[1]); }
		var start = this.number;
		var end = start + offset;
		var step = ((end - start) / 19).toFixed(0) * 1;
		var index = 0;
		var self = this;
		this.execTimer = setInterval(function() {
			var next = start + step * index++;
			if (next < end) {
				self.number = next;
				self.$element.text(self.formatNumber(self.number));
			} else {
				self.number = end;
				self.$element.text(self.formatNumber(self.number));
				window.clearInterval(self.execTimer);
				self.execTimer = null;
			}
		}, this.options.duration);
	};

	Accumulate.prototype.bind = function () {
		var self = this;
		if (this.options.runOnVisible) {
			$(window).scroll(function() {
				var max = self.$element.offset().top;
				var min = max - $(window).height();
				var scrollTop = $(window).scrollTop();
				if (scrollTop < min || scrollTop > max) {
					self.visible = false;
				} else {
					if (!self.visible) { self.run(); }
					self.visible = true;
				}
			});
		}
	};
	Accumulate.prototype.init = function () {
		if (!this.options.auto || this.options.pause || this.waitTimer != null) { return; }
		var interval = this.options.interval;
		if (interval == undefined) { return; }
		if ($.isArray(interval)) { interval = Randoms.number(interval[0], interval[1]); }
		var self = this;
		this.waitTimer = window.setTimeout(function() {
			self.waitTimer = null;
			self.run();
			self.init();
		}, interval);
	};

	function Plugin(options) {
		return this.each(function () {
			var $this = $(this);
			var data = $this.data("bs.accumulate");
			if (!data) {
				var o = $.extend({}, $this.zoptions(), $.isPlainObject(options) && options);
				$this.data("bs.accumulate", (data = new Accumulate(this, o)));
			}
			if ($.dt.isString(options)) { data[options](); }
		});
	};

	var old = $.fn.accumulate;

	$.fn.accumulate = Plugin;
	$.fn.accumulate.Constructor = Accumulate;

	$.fn.accumulate.noConflict = function () {
		$.fn.accumulate = old;
		return this;
	};

}(jQuery);
